<?php
/*
 * Главная страница
 */
class Page extends BasicModel {
	public function __construct(Core $Site) {
		parent::__construct($Site);
	}
	public function makePage() {
		$Alph = $Cities = $District = $Area = NULL;
		// настройка параметров кэширования
		$this->Config->Cache['district.page']['CACHE_NAME']	.= $this->URL->Page['ID'];
		$this->MySQL->CacheSetConfig($this->Config->Cache['district.page']);
		// загрузка кэша
		$this->MySQL->CacheLoad();
		// район
		if (!$this->MySQL->CacheIsSet('DISTRICT')) {
			$District = $this->MySQL->Select('SELECT `ID`, `NAME`, `AREA_ID`, `PAGE_H1` FROM `{p}districts` WHERE `ID` = '.$this->URL->Page['ID']);
			if ($District) {$this->MySQL->CacheWrite($District, 'DISTRICT');}
			else {$this->Site->go404();}
		} else {$District = $this->MySQL->CacheGet('DISTRICT', TRUE);}
		// область
		if (!$this->MySQL->CacheIsSet('AREA')) {
			$Area = $this->MySQL->Select('SELECT `a`.`ID`, `NAME`, `URL` FROM `{p}areas` `a` INNER JOIN `{p}urls` `u` ON `u`.`ID` = `a`.`ID` WHERE `a`.`ID` = '.$District['AREA_ID'].' AND `URL_TYPE` = 1');
			if ($Area) {$this->MySQL->CacheWrite($Area, 'AREA');}
			else {$this->Site->go404();}
		} else {$Area = $this->MySQL->CacheGet('AREA', TRUE);}
		// города
		if (!$this->MySQL->CacheIsSet('CITIES')) {
			$AlphArray = array(1=>'А', 2=>'Б', 3=>'В', 4=>'Г', 5=>'Д', 6=>'Е', 7=>'Ё', 8=>'Ж', 9=>'З', 10=>'И', 11=>'Й', 12=>'К', 13=>'Л', 14=>'М', 15=>'Н', 16=>'О', 17=>'П', 18=>'Р', 19=>'С', 20=>'Т', 21=>'У', 22=>'Ф', 23=>'Х', 24=>'Ц', 25=>'Ч', 26=>'Ш', 27=>'Щ', 28=>'Ы', 29=>'Э', 30=>'Ю', 31=>'Я');
			$iAlph	= 1;
			$cAlph	= 0;
			$Select	= $this->MySQL->Select('SELECT `c`.`ID`, `NAME`, `URL` FROM `{p}cities` `c` INNER JOIN `{p}urls` `u` ON `u`.`ID` = `c`.`ID` WHERE `DISTRICT_ID` = '.$District['ID'].' AND `URL_TYPE` = 3 ORDER BY `NAME`', MySQL::SQuery);
			$Cities	= array('COUNT'	=> 0);
			while(!is_null($City = $this->MySQL->FetchArray($Select))) {
				while($iAlph != 0) {
					if (mb_strpos($City['NAME'], $AlphArray[$iAlph]) === 0) {break;}
					if ($cAlph > 0) {$Alph[$iAlph] = $Cities['COUNT'];}
					$cAlph = 0;
					++$iAlph;
					if ($iAlph == 32) {$iAlph = 0;}
				}
				$Cities[] = $City;
				++$cAlph;
				++$Cities['COUNT'];
			}
			if ($cAlph > 0) {$Alph[$iAlph] = $Cities['COUNT'];}
			if ($Cities) {$this->MySQL->CacheWrite($Cities, 'CITIES');$this->MySQL->CacheWrite($Alph, 'ALPH');}
		} else {$Cities = $this->MySQL->CacheGet('CITIES', TRUE);$Alph = $this->MySQL->CacheGet('ALPH', TRUE);}
		// сохранение кэша
		//$this->MySQL->CacheSave();
		// настройка страницы
		$this->Template->setTitle($Area['NAME']); // тайтл
		$this->Template->setLoadPage('district.page'); // шаблон
		$this->Template->setPageElements(array ( // элементы страницы
			'AREA'			=> $Area,
			'DISTRICT'	=> $District,
			'CITIES'		=> $Cities,
			'ALPH'			=> $Alph
		));
		$this->Template->addCrumbs(array (
			'/'	=> $this->Language->LText['MAIN'],
			$Area['URL']	=> $Area['NAME'],
			'' => $District['NAME']
		));
		return 0;
	}
}
?>